﻿using System;
using System.Data;
using System.Xml;
using ZYCommon;
  
namespace TextEditor
{
    
 

    public class KB_Item : ZYDBRecordBase, IListIndex
    {
        private int intItemSEQ = -1;
        private int intItemStyle = 0;
        private int intListIndex = 0;
        private KB_List myOwnerList = null;
        public static string strDeptment = " ";
        private string strItemDesc = " ";
        private string strItemText = " ";
        private string strItemValue = " ";
        private static string[] strKBItemStyleDesc = null;
        private string strKBSEQ = null;

        public static bool CheckItemText(string strText)
        {
            if (strText == null)
            {
                return false;
            }
            strText = strText.Trim();
            if ((strText.Length == 0) || (strText.Length > 100))
            {
                return false;
            }
            return true;
        }

        public override ZYDBRecordBase CreateNewRecord()
        {
            return new KB_Item();
        }

        public override bool CustomDelete(IDbCommand myCmd, ZYDBConnection conn)
        {
            if (this.isTemplate() && StringCommon.IsInteger(this.strItemValue))
            {
                myCmd.CommandText = "Delete From ET_Document Where ObjectID = '" + this.strItemValue + "'";
                myCmd.ExecuteNonQuery();
            }
            myCmd.CommandText = "Delete From KB_Item Where ITEM_SEQ = " + this.intItemSEQ.ToString();
            myCmd.ExecuteNonQuery();
            return true;
        }

        public override bool FromReader(IDataReader myReader, ZYDBConnection conn)
        {
            if ((myReader != null) && (myReader.FieldCount >8))//myReader.FieldCount == 8
            {
                this.intItemSEQ = Convert.ToInt32(myReader[0]);
                this.strKBSEQ = myReader.IsDBNull(8) ? null : myReader.GetDecimal(8).ToString(); //; myReader.GetString(1);
                this.strItemText = myReader.IsDBNull(3) ? " " : myReader.GetString(3);
                this.strItemValue = myReader.IsDBNull(7) ? " " : myReader.GetString(7);
               // this.intListIndex = myReader.IsDBNull(10) ? -1 : Convert.ToInt32(myReader[10]);
                //this.strItemDesc = myReader.IsDBNull(4) ? " " : myReader.GetString(4);
                //if (myReader[7] != null)
                //    this.intListIndex = myReader.IsDBNull(5) ? -1 : Convert.ToInt32(myReader[7]);
                //else
                //    this.intListIndex = 0;
                if (myReader[6]!=null)
                   this.intItemStyle = myReader.IsDBNull(9) ? 0 : Convert.ToInt32(myReader[9]);
                //strDeptment = myReader.IsDBNull(7) ? " " : myReader.GetString(7);
                //if (this.intItemStyle < 0)
                //{
                //    this.intItemStyle = 0;
                //}
                return true;
            }
            return false;
        }
        //public override bool FromReader(IDataReader myReader, ZYDBConnection conn)
        //{
        //    if ((myReader != null) && (myReader.FieldCount == 8))
        //    {
        //        this.intItemSEQ = Convert.ToInt32(myReader[0]);
        //        this.strKBSEQ = myReader.IsDBNull(1) ? null : myReader.GetString(1);
        //        this.strItemText = myReader.IsDBNull(2) ? " " : myReader.GetString(2);
        //        this.strItemValue = myReader.IsDBNull(3) ? " " : myReader.GetString(3);
        //        this.strItemDesc = myReader.IsDBNull(4) ? " " : myReader.GetString(4);
        //        this.intListIndex = myReader.IsDBNull(5) ? -1 : Convert.ToInt32(myReader[5]);
        //        this.intItemStyle = myReader.IsDBNull(6) ? 0 : Convert.ToInt32(myReader[6]);
        //        strDeptment = myReader.IsDBNull(7) ? " " : myReader.GetString(7);
        //        if (this.intItemStyle < 0)
        //        {
        //            this.intItemStyle = 0;
        //        }
        //        return true;
        //    }
        //    return false;
        //}

        public bool FromXML(XmlElement myElement)
        {
            this.intItemSEQ = Convert.ToInt32(myElement.GetAttribute("seq"));
            this.strKBSEQ = myElement.GetAttribute("kb");
            this.strItemText = myElement.GetAttribute("text");
            this.strItemValue = myElement.GetAttribute("value");
            this.strItemDesc = myElement.GetAttribute("desc");
            this.intListIndex = Convert.ToInt32(myElement.GetAttribute("index"));
            this.intItemStyle = Convert.ToInt32(myElement.GetAttribute("style"));
            if (this.intItemStyle < 0)
            {
                this.intItemStyle = 0;
            }
            return true;
        }

        public static string[] GetKBItemStyleDescList()
        {
            if (strKBItemStyleDesc == null)
            {
                strKBItemStyleDesc = "普通列表项目,动态加载,SQL文本,SQL参数,SQL查询,SQL更新,退出,格式化时间,获得参数,设置参数,模板,文本框单位".Split(",".ToCharArray());
            }
            return strKBItemStyleDesc;
        }

        public static string GetSelectSQL()
        {
            return "Select * from Table_E";//"Select ITEM_SEQ, KB_SEQ,ITEM_TEXT,ITEM_VALUE,ITEM_DESC ,ListIndex , Item_Style,OWNERSECTION FROM KB_ITEM ";
        }

        public override string GetTableName()
        {
            return "Table_E";
        }

        public static int IndexToKBItemStyle(int index)
        {
            if (index < 0)
            {
                index = 0;
            }
            switch (index)
            {
                case 0:
                    return 0;

                case 1:
                    return 1;

                case 2:
                    return 11;

                case 3:
                    return 12;

                case 4:
                    return 13;

                case 5:
                    return 14;

                case 6:
                    return 0x13;

                case 7:
                    return 0x10;

                case 8:
                    return 0x11;

                case 9:
                    return 0x12;

                case 10:
                    return 100;

                case 11:
                    return 0x66;
            }
            return 0;
        }

        public bool isCreated()
        {
            return (this.intItemStyle == 1);
        }

        public override bool isKeyEnable()
        {
            return (this.intItemSEQ >= 0);
        }

        public bool isLink()
        {
            return (this.intItemStyle == 0x65);
        }

        public bool isNormalItem()
        {
            return (this.intItemStyle <= 1);
        }

        public bool isSystemItem()
        {
            return ((this.intItemStyle >= 11) && (this.intItemStyle <= 0x13));
        }

        public bool isTemplate()
        {
            return (this.intItemStyle == 100);
        }

        public bool isUnit()
        {
            return (this.intItemStyle == 0x66);
        }

        public static string KBItemStyleToDesc(int Style)
        {
            string[] strItems = GetKBItemStyleDescList();
            int index = KBItemStyleToIndex(Style);
            if ((index >= 0) && (index < strItems.Length))
            {
                return strItems[index];
            }
            return ("未知类型" + Style.ToString());
        }

        public static int KBItemStyleToIndex(int Style)
        {
            switch (Style)
            {
                case -1:
                    return 0;

                case 0:
                    return 0;

                case 1:
                    return 1;

                case 11:
                    return 2;

                case 12:
                    return 3;

                case 13:
                    return 4;

                case 14:
                    return 5;

                case 0x10:
                    return 7;

                case 0x11:
                    return 8;

                case 0x12:
                    return 9;

                case 0x13:
                    return 6;

                case 100:
                    return 10;

                case 0x66:
                    return 11;
            }
            return -1;
        }

        public override bool SetAllocKeyCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = " Select Max(ITEM_SEQ)+1 FROM KB_ITEM";
            return true;
        }

        public override bool SetDeleteCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Delete From KB_Item Where ITEM_SEQ = " + this.intItemSEQ.ToString();
            return true;
        }

        public override bool SetInsertCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Insert Into KB_Item ( ITEM_SEQ, KB_SEQ,ITEM_TEXT,ITEM_VALUE,ITEM_DESC,ListIndex,Item_Style,OWNERSECTION) VALUES(?,?,?,?,?,?,?,?)";
            myCmd.Parameters.Clear();
            ZYDBConnection.AddParameter(myCmd, this.intItemSEQ);
            ZYDBConnection.AddParameter(myCmd, this.KBSEQ);
            ZYDBConnection.AddParameter(myCmd, this.strItemText);
            ZYDBConnection.AddParameter(myCmd, this.strItemValue);
            ZYDBConnection.AddParameter(myCmd, this.strItemDesc);
            ZYDBConnection.AddParameter(myCmd, this.intListIndex);
            ZYDBConnection.AddParameter(myCmd, this.intItemStyle);
            ZYDBConnection.AddParameter(myCmd, strDeptment);
            return true;
        }

        public override bool SetKeyWord(string strKey)
        {
            bool CS0000000300000000=false;
            try
            {
                this.intItemSEQ = Convert.ToInt32(strKey);
                CS0000000300000000 = true;
            }
            catch
            {
            }
            return CS0000000300000000;
        }

        public override bool SetSelectAllCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select ITEM_SEQ, KB_SEQ,ITEM_TEXT,ITEM_VALUE,ITEM_DESC ,ListIndex , Item_Style FROM KB_ITEM Order By KB_SEQ , ListIndex ";
            return true;
        }

        public static void SetSelectAllCommandWidthSection(IDbCommand myCmd, string strSection)
        {
            myCmd.CommandText = "Select KB_ITEM.ITEM_SEQ, KB_ITEM.KB_SEQ,KB_ITEM.ITEM_TEXT,KB_ITEM.ITEM_VALUE,KB_ITEM.ITEM_DESC ,KB_ITEM.ListIndex , KB_ITEM.Item_Style FROM KB_ITEM , KBSection where kb_item.kb_seq = kbsection.kb_seq and kbsection.ownersection = ? Order By kb_item.KB_SEQ , kb_item.ListIndex ";
            myCmd.Parameters.Clear();
            ZYDBConnection.AddParameter(myCmd, strSection);
        }

        public override bool SetSelectCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select ITEM_SEQ, KB_SEQ,ITEM_TEXT,ITEM_VALUE,ITEM_DESC ,ListIndex , Item_Style FROM KB_ITEM Where KB_SEQ = " + this.strKBSEQ.ToString() + " Order By ListIndex ";
            return true;
        }

        public override bool SetSelectCommandForOneRecord(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select ITEM_SEQ, KB_SEQ,ITEM_TEXT,ITEM_VALUE,ITEM_DESC , ListIndex , Item_Style FROM KB_ITEM Where Item_seq = " + this.intItemSEQ.ToString();
            return true;
        }

        public static void SetSelectSEQCommand(IDbCommand myCmd)
        {
            myCmd.CommandText = "Select ITEM_SEQ From KB_Item Order by ITEM_SEQ ";
        }

        public override bool SetTestExistCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select Item_seq From KB_ITEM WHERE ITEM_SEQ =" + this.intItemSEQ.ToString();
            return true;
        }

        public override bool SetUpdateCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Update KB_ITEM SET KB_SEQ = ? , ITEM_TEXT = ? , ITEM_VALUE = ? , ITEM_DESC=? , ListIndex = ? , Item_Style = ? WHERE ITEM_SEQ= " + this.intItemSEQ.ToString();
            myCmd.Parameters.Clear();
            ZYDBConnection.AddParameter(myCmd, this.KBSEQ);
            ZYDBConnection.AddParameter(myCmd, this.strItemText);
            ZYDBConnection.AddParameter(myCmd, this.strItemValue);
            ZYDBConnection.AddParameter(myCmd, this.strItemDesc);
            ZYDBConnection.AddParameter(myCmd, this.intListIndex);
            ZYDBConnection.AddParameter(myCmd, this.intItemStyle);
            return true;
        }

        public bool ToXML(XmlElement myElement)
        {
            myElement.SetAttribute("seq", this.intItemSEQ.ToString());
            myElement.SetAttribute("kb", this.strKBSEQ);
            myElement.SetAttribute("text", this.strItemText);
            myElement.SetAttribute("value", this.strItemValue);
            myElement.SetAttribute("desc", this.strItemDesc);
            myElement.SetAttribute("index", this.intListIndex.ToString());
            myElement.SetAttribute("style", this.intItemStyle.ToString());
            return true;
        }

        public string ItemDesc
        {
            get
            {
                return this.strItemDesc;
            }
            set
            {
                if ((this.strItemDesc != value) && (value != null))
                {
                    this.strItemDesc = value;
                    base.SetModified();
                }
            }
        }

        public int ItemSEQ
        {
            get
            {
                return this.intItemSEQ;
            }
            set
            {
                this.intItemSEQ = value;
            }
        }

        public int ItemStyle
        {
            get
            {
                return this.intItemStyle;
            }
            set
            {
                if (this.intItemStyle != value)
                {
                    this.intItemStyle = value;
                    base.SetModified();
                }
            }
        }

        public string ItemText
        {
            get
            {
                return this.strItemText;
            }
            set
            {
                if ((this.strItemText != value) && (value != null))
                {
                    if (this.strItemText == this.strItemValue)
                    {
                        this.strItemValue = value;
                    }
                    this.strItemText = value;
                    base.SetModified();
                }
            }
        }

        public string ItemValue
        {
            get
            {
                return this.strItemValue;
            }
            set
            {
                if ((this.strItemValue != value) && (value != null))
                {
                    this.strItemValue = value;
                    base.SetModified();
                }
            }
        }

        public string KBSEQ
        {
            get
            {
                if (this.myOwnerList == null)
                {
                    return this.strKBSEQ;
                }
                return this.myOwnerList.SEQ;
            }
            set
            {
                this.strKBSEQ = value;
                base.SetModified();
            }
        }

        public int ListIndex
        {
            get
            {
                return this.intListIndex;
            }
            set
            {
                if (this.intListIndex != value)
                {
                    this.intListIndex = value;
                    base.SetModified();
                }
            }
        }

        public KB_List OwnerList
        {
            get
            {
                return this.myOwnerList;
            }
            set
            {
                this.myOwnerList = value;
            }
        }
    }
}

